import { systemShowLoading, systemHideLoading } from '../actions/index.js';

export default store => next => action => {
  const { dispatch, getState } = store;
  const { promise, ...rest } = action;
  if (!promise) {
    return next(action);
  }
  /*请求时回调方法*/
  const onSuccessed = result => {
    dispatch(systemHideLoading());
    next({
      ...rest,
      result,
    });
  }
  /*请求失败时的回调方法*/
  const onFailed = error => {
    dispatch(systemHideLoading());
    next({
      ...rest,
      error
    });
  }

  dispatch(systemShowLoading());

  return promise().then(onSuccessed).catch(onFailed);
}